/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejbs;

import dtos.FuncBalcaoDTO;
import entities.Farmacia;
import entities.FuncBalcao;
import exceptions.EntidadeExistenteException;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Rafa
 */
@Stateless
public class FuncBalcaoBean {

    @PersistenceContext
    private EntityManager em;

    @EJB
    private FuncionarioBean funcBean;

    public void criarFuncBalcao(String username, String password, String name, String email, String farmacia) throws EntidadeExistenteException {
        try {
            if (funcBean.existeFuncionario(username)) {
                throw new EntidadeExistenteException("Esse admin já existe");
            }
            em.persist(new FuncBalcao(username, name, password, email, em.find(Farmacia.class, farmacia)));

        } catch (EntidadeExistenteException e) {
            throw e;
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    public FuncBalcaoDTO getFuncBalcao(String nome) {
        FuncBalcao f = em.find(FuncBalcao.class, nome);
        return new FuncBalcaoDTO(f.getNome(), f.getEmail(), f.getFarmacia().getNome());
    }
}
